package com.showde.shoppingmall.activity;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.ProductListAdapter;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.ProductDBCommand;
import com.showde.shoppingmall.domain.Product;
import com.showde.shoppingmall.utils.ActivityDispatch;

public class ProductListActivity extends BaseActivity {

	private static final String TAG = "ProductListActivity";

	private static final int MSG_REFRESH = 0;

	private ListView productsListView;
	private ProductListAdapter productListAdapter;
	private List<Product> products;

	private ProductDBCommand productDBCommand;
	private String productCategoryId;

	private boolean needRefresh = false;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESH:
				refreshData();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
	}

	@Override
	protected void onResume() {
		super.onResume();
		fetchProductFromDB();
		refreshData();
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	protected void initParam() {

		Bundle bundle = intent.getExtras();
		productCategoryId = bundle
				.getString(ActivityDispatch.PRODUCT_CATEGORY_ID);
		Log.d(TAG, "productCategoryId:" + productCategoryId);
		if (productCategoryId != null) {
			fetchProductFromNet();
		}
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_productlist);
		productsListView = (ListView) findViewById(R.id.lst_product);
		productsListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Product product = products.get(position);
						Bundle bundle = new Bundle();
						bundle.putString(ActivityDispatch.PRODUCT_ID,
								product.getProductID());
						ActivityDispatch.openNewActivity(
								ProductListActivity.this,
								ProductDetailsActivity.class,
								ActivityDispatch.getActivityGroup(groupTag),
								"", bundle, groupTag);
					}
				});
	}

	private void fetchProductFromDB() {
		if (productDBCommand == null) {
			productDBCommand = new ProductDBCommand(this);
		}
		products = productDBCommand.queryProductByCategoryId(productCategoryId);
		Log.d(TAG, "products:" + products);
	}

	private void fetchProductFromNet() {
		ProductLogic.fetchProductByCategoryId(this, productCategoryId,
				new UICallbackInterface() {

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(CallbackData data) {
						if (data != null && ((List<Product>) data.data) != null) {
							data = null;
							fetchProductFromDB();
							handler.sendEmptyMessage(MSG_REFRESH);
						}
					}

					@Override
					public void onError(String errorMsg) {
						Log.d(TAG, "fetchProductFromNet:" + errorMsg);
					}
				});
	}

	private void refreshData() {
		Log.d(TAG, "refreshData");
		productListAdapter = (ProductListAdapter) productsListView.getAdapter();
		if (productListAdapter == null) {
			productListAdapter = new ProductListAdapter(this, products);
			productsListView.setAdapter(productListAdapter);
		} else {
			productListAdapter.resetData(products);
		}
		productListAdapter.notifyDataSetChanged();
	}

}
